//城市选择
var app = getApp();
Page({
    data: {
        provincelist:[],
        cityList:[],
        areaList:[],
        shengArr: [], //省级数组
        shiArr: [],   //城市数组
        quArr: [],    //区数组
        shengId: [],  //省级id数组
        shiId: [],    //城市id数组
        quId: [],     //区id数组
        provinceArr:[],
        cityArr: [],
        areaArr: [],
        provinceId: [],
        cityId: [],
        areaId: [],
        sheng:0,
        city:0,
        area:0,
        code:0,
        cartId:0,
        addrId:0,
        multiIndex: [0,0,0],
        regionIndex:[0,0,0],
        multiArray:[], 
        isShowToast: false, //toast默认不显示
        isinitial: 0,  //地区默认显示
    },
    //初始化获取地区数据
    getprovincelist: function (e) {
        var that = this;
        app.request("UserAddressService", "getAreaList",
            {
                rtype: 1//地区类型，0国家，1省级，2地市，3区县
            }, function (data) {
                that.setData({
                    provincelist: data.list
                })
                that.getCitysList(e);
            });
    },
    getCitysList: function (e){
        var that = this;
        app.request("UserAddressService", "getAreaList",
            {
                rtype: 2//地区类型，0国家，1省级，2地市，3区县
            }, function (data) {
                that.setData({
                    cityList: data.list
                })
                that.getAreaList(e);
            });
    },
    getAreaList: function (e) {
        var that = this;
        app.request("UserAddressService", "getAreaList",
            {
                rtype: 3//地区类型，0国家，1省级，2地市，3区县
            }, function (data) {
                that.setData({
                    areaList: data.list
                })
                that.getaddress(e);
            });
    },
    getaddress: function (even){
      var cityArr = [];
      var cityIdArr = [];
      var proArr = [];
      var proIdArr = [];
      var provinceList = this.data.provincelist;
      var cityList = this.data.cityList;
      var areaList = this.data.areaList;
      var a=0,b=0,c=0;
      var Index=[];
      //省份
      for (var i = 0; i < provinceList.length; i++) {
        proArr.push(provinceList[i].region_name)
        proIdArr.push(provinceList[i].region_id)
      }
      if (even === undefined){
        var provinceId = proIdArr[0];
      }else{
        var provinceId = even.province;
        for (var i = 0; i < proIdArr.length; i++) {
          if (proIdArr[i] == provinceId) { a = i }
        }
      }
      //城市
      for (var i = 0; i < cityList.length; i++) {
        if (cityList[i].parent_id == provinceId) {
          cityArr.push(cityList[i].region_name)
          cityIdArr.push(cityList[i].region_id)
        }
      }
      if (even === undefined){
        var cityId = cityIdArr[0];
      }else{
        var cityId = even.city;
        for (var i = 0; i < cityIdArr.length; i++) {
          if (cityIdArr[i] == cityId) { b = i }
        }
      }
      //区域
      var areaArr = [];
      var areaIdArr = [];
      for (var i = 0; i < areaList.length; i++) {
        if (areaList[i].parent_id == cityId) {
          areaIdArr.push(areaList[i].region_id)
          areaArr.push(areaList[i].region_name)
        }
      }
      if (even !== undefined){
        for (var i = 0; i < areaIdArr.length; i++) {
          if (areaIdArr[i] == even.district) { c = i }
        }
        Index=[a,b,c];
      }
      this.setData({
        //地区选择器显示
        multiArray: [proArr, cityArr, areaArr],
        multiIndex: Index,
        shengArr: proArr,
        shengId: proIdArr,
        shiArr: cityArr,
        shiId: cityIdArr,
        quArr: areaArr,
        quId: areaIdArr,
        //地址页面显示
        regionIndex: Index,
        provinceArr: proArr,
        cityArr: cityArr,
        areaArr: areaArr,
        provinceId: proIdArr,
        cityId: cityIdArr,
        areaId: areaIdArr,
      })
    },
    check:function(e){
      var regNum = new RegExp('[0-9]', 'g');//判断用户输入的是否为数字
      var rsNum = regNum.exec(e.detail.value);
      if (!rsNum) {
        this.setData({
          count: 1500,
          toastText: '号码为数字，请重新输入！'
        });
        this.showToast();  
      }
    },
    formSubmit: function (e) {
        var adds = e.detail.value;
        var cartId = this.data.cartId;
        var addressType = this.data.addrId;
        if (adds.name =="" || adds.phone ==""){
          this.setData({
            count: 1500,
            toastText: '请完善收货信息!'
          });
          this.showToast();
        }else{
          if (addressType == 0) { //新增用户常用地址
            app.request("UserAddressService", "addUserAddress",
              {
                user_id: app.d.userId,
                receiver: adds.name,
                tel: adds.phone,
                sheng: this.data.provinceId[this.data.regionIndex[0]],
                city: this.data.cityId[this.data.regionIndex[1]],
                quyu: this.data.areaId[this.data.regionIndex[2]],
                shengName: this.data.provinceArr[this.data.regionIndex[0]],
                shiName: this.data.cityArr[this.data.regionIndex[1]],
                quyuName: this.data.areaArr[this.data.regionIndex[2]],
                adds: adds.address,
                code: this.data.code,
                country: 0,
              }, function (data) {
                wx.showToast({
                  title: '保存成功！',
                  duration: 2000
                });
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.renovate();
                wx.navigateBack();
              });
          } else {  //修改用户常用地址
            app.request("UserAddressService", "modUserAddress",
              {
                uid: app.d.userId,
                addrId: this.data.addrId,
                adds: adds.address,
                sheng: this.data.provinceId[this.data.regionIndex[0]],
                city: this.data.cityId[this.data.regionIndex[1]],
                quyu: this.data.areaId[this.data.regionIndex[2]],
                shengName: this.data.provinceArr[this.data.regionIndex[0]],
                shiName: this.data.cityArr[this.data.regionIndex[1]],
                quyuName: this.data.areaArr[this.data.regionIndex[2]],
                code: this.data.code,
                receiver: adds.name,
                tel: adds.phone,
                country: 0,
              }, function (data) {
                wx.showToast({
                  title: '保存成功！',
                  duration: 2000
                });
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.renovate();
                wx.navigateBack();
              });
          }
        }
    },
    onLoad: function (options) {
        // 生命周期函数--监听页面加载
        var that = this;
        if (options.address === undefined){
          that.getprovincelist();
        }else{
          var addressItem = JSON.parse(options.address);
          that.getprovincelist(addressItem);
          var address = addressItem.full_address;
          var address_list = address.split(" ", 4);
          if (options.editType == 0) {  //editType：0 修改 1 新增
            wx.setNavigationBarTitle({ title: '修改地址' })
          }
          that.setData({
            cartId: options.cartId,
            addrId: addressItem.address_id,
            user_phone: addressItem.tel,
            user_name: addressItem.consignee,
            detailed: address_list[address_list.length-1],
            isinitial:1,
          })
        }
    },
    bindMultiPickerChange: function (e) {
      var region = this.data.multiIndex;
      if (region.length == 0){
        region = [0,0,0];
      }
      this.setData({
          regionIndex: region,
          provinceArr: this.data.shengArr,
          cityArr: this.data.shiArr,
          areaArr: this.data.quArr,
          provinceId: this.data.shengId,
          cityId: this.data.shiId,
          areaId: this.data.quId,
          isinitial:1,
      })
    },
    bindMultiPickerColumnChange: function (e) {
        var data = {
            multiArray: this.data.multiArray,
            multiIndex: this.data.multiIndex,
        };
        if (data.multiIndex.length == 0){
          data.multiIndex=[0,0,0];
        }
        var cityArr = [];
        var cityIdArr = [];
        var proArr = [];
        var proIdArr = [];
        var areaArr = [];
        var areaIdArr = [];
        var provinceList = this.data.provincelist;
        var cityList = this.data.cityList;
        var areaList = this.data.areaList;
        data.multiIndex[e.detail.column] = e.detail.value;
        switch (e.detail.column) {
            case 0:
                var p = data.multiIndex[0];
                for (var i = 0; i < provinceList.length; i++){
                    proArr.push(provinceList[i].region_name)
                    proIdArr.push(provinceList[i].region_id)
                }
                var provinceId = proIdArr[p];
                for (var i = 0; i < cityList.length; i++) {
                    if (cityList[i].parent_id == provinceId) {
                        cityArr.push(cityList[i].region_name)
                        cityIdArr.push(cityList[i].region_id)
                    }
                }
                var cityId = cityIdArr[0];
                for (var i = 0; i < areaList.length; i++) {
                    if (areaList[i].parent_id == cityId) {
                        areaIdArr.push(areaList[i].region_id)
                        areaArr.push(areaList[i].region_name)
                    }
                }
                this.setData({
                  shengArr: proArr,
                  shengId: proIdArr,
                  shiArr: cityArr,
                  shiId: cityIdArr,
                  quArr: areaArr,
                  quId: areaIdArr,
                })
                data.multiArray[1] = cityArr;
                data.multiArray[2] = areaArr;
                data.multiIndex[1] = 0;
                data.multiIndex[2] = 0;
                break;
            case 1:
                var areaArr = [];
                var areaIdArr = [];
                var c = data.multiIndex[1];
                var cityId = this.data.shiId[c];
                for (var i = 0; i < areaList.length; i++) {
                    if (areaList[i].parent_id == cityId) {
                        areaIdArr.push(areaList[i].region_id)
                        areaArr.push(areaList[i].region_name)
                    }
                }
                this.setData({
                  quArr: areaArr,
                  quId: areaIdArr,
                })
                data.multiArray[2] = areaArr;
                data.multiIndex[2] = 0;
                break;
        }
        this.setData(data);
    },
    //自定义toast
    showToast: function () {
      var that = this;
      // toast时间  
      that.data.count = parseInt(that.data.count) ? parseInt(that.data.count) : 3000;
      // 显示toast  
      that.setData({
        isShowToast: true,
      });
      // 定时器关闭  
      setTimeout(function () {
        that.setData({
          isShowToast: false
        });
      }, that.data.count);
    }, 
})